<template>
	<div class="hy-container">
		<HyPageHeader :title="title" :buttonData="buttonData">
			<Detail v-if="title === '机构详情'" :detailData="detailData" />
			<HyCard v-else title="基本信息">
				<HyForm :hyFormConfig="hyFormConfig" :hyFormFields="hyFormFields" ref="hyFormRef" />
			</HyCard>
		</HyPageHeader>
	</div>
</template>
<script setup lang="ts">
import HyForm from '/@/components/HyForm/index.vue';
import HyCard from '/@/components/HyCard/index.vue';
import { useDict } from '/@/hooks/dict';
import HyPageHeader from '/@/components/HyPageHeader/index.vue';
import { institutionAddApi, institutionInfoDetailApi } from '/@/api/expertDatabase/institution';
import { ElLoading } from 'element-plus';
import mittBus from '/@/utils/mitt';
import Detail from '../components/Detail/index.vue';
import { getRouterParams } from '/@/utils/hyFunc';

const formType: string = inject('formType', 'submit');

// 定义字典
const { business_status, yes_no_type } = useDict('business_status', 'yes_no_type');

//配置基本信息表单
const hyFormFields = computed(() => {
	return {
		expertInfoId: {
			type: 'input',
			hidden: true,
		},
		unifiedSocialCreditcode: {
			label: '统一社会信用代码',
			type: 'input',
			required: true,
			rules: [
				{
					pattern: /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/,
					message: '请输入正确的统一社会信用代码',
					trigger: 'blur',
				},
			],
		},
		institutionName: {
			label: '第三方机构名称',
			type: 'input',
			required: true,
		},
		contacts: {
			label: '联系人',
			required: true,
			type: 'input',
		},
		telephone: {
			label: '联系人电话',
			type: 'input',
			required: true,
			maxlength: 11,
			rules: [
				{
					pattern: /^((0\d{2,3}-\d{7,8})|(1[3456789]\d{9}))$/,
					message: '请输入正确的手机号码格式',
					trigger: 'blur',
				},
			],
		},
		incorporate: {
			label: '成立日期',
			type: 'date',
			showFormat: 'YYYY-MM-DD',
			valueFormat: 'YYYY-MM-DD',
			required: true,
		},
		registeredCapital: {
			label: '注册资本',
			type: 'inputFunny',
			required: true,
		},
		businessStatus: {
			label: '经营状态',
			type: 'select',
			required: true,
			options: business_status.value,
		},
		registeredAddress: {
			label: '注册地址',
			type: 'textarea',
			required: true,
		},
		businessScope: {
			label: '经营范围',
			type: 'textarea',
		},
		isConfidentialQualifications: {
			label: '是否拥有涉密机制',
			type: 'select',
			required: true,
			options: yes_no_type.value,
		},
		serviceSituation: {
			label: '服务情况',
			type: 'textarea',
		},
		businessLicense: {
			label: '',
			type: 'input',
			hidden: true,
		},
		businessLicenseFiles: {
			label: '营业执照',
			required: true,
			type: 'upload',
			fileType: ['.jpg', '.jpeg', '.png'],
			fileSize: 200,
			fileLimit: 1,
		},
		contractAwarded: {
			label: '',
			type: 'input',
			hidden: true,
		},
		contractAwardedFiles: {
			label: '中标合同',
			required: true,
			type: 'upload',
			fileType: ['.jpg', '.jpeg', '.png'],
			fileSize: 200,
			fileLimit: 1,
		},
	};
});

//动态表单配置
const hyFormConfig = computed(() => {
	return {
		labelWidth: '120px',
		type: formType,
	};
});
const title = ref('');
const setTitle = (value: string) => {
	switch (value) {
		case 'add':
			title.value = '新增机构';
			break;
		case 'edit':
			title.value = '编辑机构';
			break;
		case 'view':
			title.value = '机构详情';
			break;
		default:
			break;
	}
};
const router = useRouter();
const detailData = ref({});
watch(
	[() => router],
	() => {
		const { operateType = '', institutionInfoId = '' } = getRouterParams();
		setTitle(operateType);
		nextTick(() => getDetail(institutionInfoId, operateType));
	},
	{ immediate: true, deep: true }
);
//获取详情
const getDetail = (institutionInfoId: string, operateType: string) => {
	if (operateType === 'view' || operateType === 'edit') {
		const loading = ElLoading.service({
			lock: true,
			text: '获取数据中...',
		});
		institutionInfoDetailApi({ institutionInfoId })
			.then((res) => {
				//编辑，设置表单数据
				if (operateType !== 'view') {
					hyFormRef.value.hyForm = res;
				}
				//查看，设置详情数据
				else {
					detailData.value = res;
				}
			})
			.finally(() => {
				loading.close();
			});
	} else {
		detailData.value = {};
	}
};
const buttonData = computed(() => {
	if (title.value === '机构详情') return [];
	return [{ name: '保存', class: 'white-button', click: () => handleSave() }];
});
const hyFormRef = ref();
const route = useRoute();
const handleSave = async () => {
	const result = await hyFormRef.value?.check();
	if (!result) return;
	const loading = ElLoading.service({
		lock: true,
		text: '提交中...',
	});
	institutionAddApi(hyFormRef.value.hyForm)
		.then(() => {
			mittBus.emit('onCurrentContextmenuClick', Object.assign({}, { contextMenuClickId: 1, ...route }));
		})
		.finally(() => {
			loading.close();
		});
};
</script>
<style scoped lang="scss"></style>
